<template>
    <div class="goods-info-container">
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :lunboList="lunboList" :is_full="false"></swiper>
                </div>
            </div>
        </div>

        <div class="mui-card">
            <div class="mui-card-header">{{ goodsInfo.title }}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>市场价：<del>￥&nbsp;{{ goodsInfo.market_price }}</del>&nbsp;&nbsp;&nbsp;销售价：<span class="sell_price">￥&nbsp;{{ goodsInfo.sell_price }}</span></p>
                    <p class="buy-num">
                        <span>购买数量：</span>
                        <numberbox @changeAddCarCount="changeAddCarCount" :max="goodsInfo.stock_quantity"></numberbox>
                    </p>
                    <mt-button type="primary" size="small">立即购买</mt-button>
                    <mt-button type="danger" size="small" @click="addToCar">加入购物车</mt-button>
                </div>
            </div>
        </div>

        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content"> 
                <div class="mui-card-content-inner">
                    <p>商品编号：{{ goodsInfo.goods_no }}</p>
                    <p>库存情况：{{ goodsInfo.stock_quantity }}</p>
                    <p>上架时间：{{ goodsInfo.add_time }}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <div>
                    <mt-button type="primary" plain size="large" @click="goDesc(id)">图文介绍</mt-button>
                </div>
                <div>
                    <mt-button type="danger" plain size="large" @click="goComment(id)">商品评论</mt-button>
                </div>
            </div>
        </div>
        <transition
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter"
        >
        <div class="ball" v-show="addToCarFlag" ref="ball"></div>
    </transition>
    <div class="test-ball"></div>
    </div>
</template>
<script>
    import swiper from '../subcomponents/swiper.vue'
    import numberbox from '../subcomponents/goods_info_number_box.vue'
    export default {
        data(){
            return {
                lunboList: [],
                goodsInfo: {},
                addCarCount: 1,
                addToCarFlag: false,
            }
        },
        created(){
            this.getLunbotu()
            this.getGoodsInfo()
        },
        methods: {
            changeAddCarCount(count){
                this.addCarCount = count
            },
            beforeEnter(el){
                el.style.transform = "translate(0, 0)"
            },
            enter(el, done){
                el.offsetWidth
                /**
                 * 小球动画优化思路
                 * 导致现象：
                 *  不同移动设备屏幕，或者移动当前设备滚动条，均会出现小球移动位置变化的情况
                 * 导致原因： 
                 *  1. 移动设备屏幕尺寸多样，小球移动位置局限在某一分辨率下。
                 *  2. 屏幕滚动，小球移动位置设置为固定。
                 * 解决办法：
                 *  getBoundingClientRect()
                 */
                
                // 获取小球在页面当中的位置
                const ballPosition = this.$refs.ball.getBoundingClientRect()
                const badagePosition = document.querySelector('#badge').getBoundingClientRect()
                let top = badagePosition.top - ballPosition.top
                let left = badagePosition.left - ballPosition.left
                el.style.transition = "all .8s cubic-bezier(.4, -0.3, 1, .68)"
                el.style.transform = `translate(${left}px, ${top}px)`
                done()
            },
            afterEnter(el){
                this.addToCarFlag = !this.addToCarFlag
            },
            addToCar(){
                this.addToCarFlag = !this.addToCarFlag
                this.$store.commit('addToCar', {
                    goods_id: this.goodsInfo.id,
                    goods_title: this.goodsInfo.title, 
                    goods_add_car_count: this.addCarCount, 
                    goods_sell_price:this. goodsInfo.sell_price,
                    goods_img_src: this.lunboList[0].src,
                    goods_stock_quantity: this.goodsInfo.stock_quantity
                })
            },
            getLunbotu(){
                this.$http
                    .get('api/getthumimages/' + this.$route.params.id)
                    .then(result => {
                        if (result.body.status === 0) {
                            this.lunboList = result.body.message
                        }
                    })
            },
            getGoodsInfo(){
                this.$http
                    .get('api/goods/getinfo/' + this.$route.params.id)
                    .then(result => {
                        if (result.body.status === 0) {
                            this.goodsInfo = result.body.message[0]
                        }
                    })
            },
            goDesc(id){
                this.$router.push({name: "goodsdesc", param:{id}})
            },
            goComment(id){
                this.$router.push({name: "goodscomment", param:{id}})
            }
        },
        components: {
            swiper,
            numberbox
        }
    }
</script>
<style lang="scss" scoped>
    .goods-info-container{
        background-color: #eee;
        overflow: hidden;
        .sell_price{
            color: red;
            font-weight: bold;
        }
        .mui-card-footer{
            display: block;
            div{
                margin-bottom: 10px;
            }
        }
        .ball{
                position: absolute;
                top: 390px;
                left: 150px;
                width: 15px;
                height: 15px;
                border-radius: 50%;
                background-color: red;
                z-index:99;
        }
    }
</style>